package com.cskaoyan.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

/**
 * 创建日期: 2022/11/22 17:14
 *
 * @author ciggar
 */
public class MybatisUtils {

    static SqlSession sqlSession;
    static {
        // 1. 获取一个SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        // 2. 获取SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = null;
        try {
            sqlSessionFactory = builder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 3. 获取SqlSession
        sqlSession = sqlSessionFactory.openSession(true);
    }


    // 获取SqlSession
    public static SqlSession getSqlSession(){

        return sqlSession;
    }
}
